﻿using Common.Buffer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Package
{
    /// <summary>
    /// 登录封包类
    /// </summary>
    public class MapleLoginPacket
    {

        /// <summary>
        /// 客户端版本检测
        /// </summary>
        /// <param name="mapleVersion"></param>
        /// <param name="sendIv"></param>
        /// <param name="recvIv"></param>
        /// <returns></returns>
        public static MaplePacket GetHello(short mapleVersion, byte[] sendIv, byte[] recvIv)
        {
            using (MaplePacketWrite buff = new MaplePacketWrite(SendPacketOpcode.版本认证))
            {
                buff.Write(0);
                //buff.WriteShort(0x0E);
                buff.WriteShort(ServerConstants.MAPLE_VERSION);
                buff.WriteMapleAsciiString(ServerConstants.MAPLE_PATCH);
                buff.Write(recvIv);
                buff.Write(sendIv);
                buff.Write(ServerConstants.MAPLE_TYPE);

                return buff;
            }
        }


        /// <summary>
        /// 获取登录错误
        /// </summary>
        /// <param name="reason"></param>
        /// <returns></returns>
        /// 
        public static MaplePacket GetLoginError(int reason)
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.登录状态))
            {
                /***
                 * 0x02:你的帐号已被封停。
                 * 0x04:密码错误!请确认!
                 * 0x05:未登录过的帐号,请确认!
                 * 0x06:系统错误,无法连接!请稍后再试!
                 * 0x07:现在连接的帐号或正在检查服务器.请再确认一下.
                 * 0x08:系统错误,无法连接!请稍后再试!
                 * 0x0A:服务器忙,无法处理你的请求。请稍后再连.
                 * 0x0B:只有20岁以上的用户可以连接。请你选择其他频道。
                 * 0x0D:现在的IP不能做MASTER连接。请你再确认一下.
                 * 0x0E:一堆韩文.然后打开韩国官方网站.
                 */
                mplew.Write(reason);
                mplew.Write(0);
                mplew.WriteInt(0);
                return mplew;
            }
        }

        /// <summary>
        /// 注册提示
        /// </summary>
        /// <param name="error"></param>
        /// <returns></returns>
        public static MaplePacket RegisterHint(int error)
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.注册提示))
            {
                /***
                 * 0x00:注册成功
                 * 0x01:注册失败
                 */
                mplew.Write(error);

                return mplew;
            }
        }

        /// <summary>
        /// 显示注册UI
        /// </summary>
        /// <returns></returns>
        public static MaplePacket ShowRegisterUI(bool isReg)
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.注册显示))
            {
                /***
                 * 0x00:本大区已暂停新帐号注册和新PT帐号登陆,
                 * 0x01:开启注册
                 * **/
                mplew.WriteBool(isReg);
                return mplew;
            }
        }

        /// <summary>
        /// 检测帐号
        /// </summary>
        /// <param name="Accounts"></param>
        /// <param name="isUse"></param>
        /// <returns></returns>
        public static MaplePacket CheckAccounts(string Accounts, bool isUse)
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.帐号检测))
            {
                mplew.WriteMapleAsciiString(Accounts);
                /***
                 * 0x00 可用该帐号
                 * 0x01 该帐号已登陆!请输入其他帐号。
                 */
                mplew.WriteBool(isUse);
                return mplew;
            }
        }

        /// <summary>
        /// 登录成功
        /// </summary>
        /// <returns></returns>
        public static MaplePacket GetAuthSuccessRequest(MapleClient client)
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.登录状态))
            {
                mplew.Write(0);//帐号登录
                mplew.WriteInt((int)client.GetID());
                mplew.Write(client.GetGender());//帐号性别
                mplew.Write(0);//管理员?
                mplew.WriteMapleAsciiString(client.GetUseName());
                mplew.WriteInt((int)client.GetID());
                mplew.Write(0);
                return mplew;
            }
        }

        /// <summary>
        /// 服务器列表结束
        /// </summary>
        /// <returns></returns>
        public static MaplePacket getEndOfServerList()
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.服务器列表))
            {
                mplew.Write(0xFF);
                return mplew;
            }
        }

        /// <summary>
        /// 服务器列表
        /// </summary>
        /// <returns></returns>
        public static MaplePacket getServerList(int ChannlSzie,int ServerID)
        {
            using (MaplePacketWrite mplew = new MaplePacketWrite(SendPacketOpcode.服务器列表))
            {
                mplew.Write(ServerID);//这里表示哪个小区.0蓝蜗牛 1蘑菇仔...

                mplew.WriteMapleAsciiString("C#服务端");//冒险岛名称
                mplew.Write(ChannlSzie);//频道数量


                for (int i = 0; i < ChannlSzie; i++)
                {
                    mplew.WriteMapleAsciiString("冒险岛-"+i);
                    mplew.WriteInt(255);//在线人数
                    mplew.Write(1);//世界ID
                    mplew.Write(i - 1);
                    mplew.Write(0);
                }


                return mplew;
            }
        }


    }
}
